#include "stdafx.h"
#include "CreatePrjDlg.h"
#include "FolderDlg.h"

CCreatePrjDlg::CCreatePrjDlg(LPCTSTR lpszInitDir)
:m_strInitDir(lpszInitDir)
{
}

CCreatePrjDlg::~CCreatePrjDlg(void)
{
}

LRESULT CCreatePrjDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	CenterWindow();
	CWindow Location = GetDlgItem(IDC_PROJECT_LOCATION);
	Location.SetWindowText(m_strInitDir);

	return TRUE;    // let the system set the focus
}

LRESULT CCreatePrjDlg::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	EndDialog(IDCANCEL);
	return 0;
}

LRESULT CCreatePrjDlg::OnOKCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	if(wID == IDOK)
	{
		CWindow PrjName = GetDlgItem(IDC_PROJECT_NAME);
		CWindow Location = GetDlgItem(IDC_PROJECT_LOCATION);
		BSTR sPrjName = NULL;
		BSTR sLocation = NULL;
		PrjName.GetWindowText(sPrjName);
		Location.GetWindowText(sLocation);
		if(sPrjName == NULL || sPrjName[0] == L'\0' || sLocation == NULL || sLocation[0] == L'\0')
		{
			MessageBox(_T("Project name or location can't be empty."), _T("Warning!"), MB_OK | MB_ICONWARNING);
			return 0;
		}
		m_sPathPrj = sLocation;
		m_sPathPrj += L"\\";
		m_sPathPrj += sPrjName;
		m_sPathPrj += L"\\";

		m_sProjectName = sPrjName;

		SysFreeString(sPrjName);
		SysFreeString(sLocation);
	}
	EndDialog(wID);
	return 0;
}

LRESULT CCreatePrjDlg::OnBrowser(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	CFolderDlg dlg(m_strInitDir);
	int nRet = dlg.DoModal(m_hWnd);

	if(nRet == IDOK)
	{
		CWindow Location = GetDlgItem(IDC_PROJECT_LOCATION);
		Location.SetWindowText(dlg.m_sPath);
	}
	return 0;
}
